package com.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.store.comstant.BasePageForm;
import com.store.core.entity.FUser;
import com.store.entity.dto.FUserDTO;
import com.store.mapper.FUserMapper;
import com.store.service.IFUserService;
import com.store.utils.BeanConvertUtils;
import com.store.utils.ListCopyUtil;
import com.store.utils.PageHelperUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author json
 * @since 2021-10-10
 */

@Slf4j
@Service
public class FUserServiceImpl extends ServiceImpl<FUserMapper, FUser> implements IFUserService {

    @Autowired
    private FUserMapper fUserMapper;

    /**
     * 翻页
     *
     * @param entityDTO
     * @param basePageForm
     * @return
     */
    @Override
    public IPage<FUserDTO> queryListPage(FUserDTO entityDTO, BasePageForm basePageForm) {
        //设置条件
        LambdaQueryWrapper<FUser> queryWrapper = new LambdaQueryWrapper();

        IPage<FUser> pageData = fUserMapper.selectPage(
             PageHelperUtil.getMPlusPageByBasePage(basePageForm), queryWrapper);
        //返回
        IPage<FUserDTO> data  = pageData.convert(entity -> BeanConvertUtils.copyProperties(entity,FUserDTO.class));
        return data;
    }

    /**
     * 列表查询
     *
     * @param entityDTO
     * @return
     */
    @Override
    public List<FUserDTO> queryList(FUserDTO entityDTO) {
        LambdaQueryWrapper<FUser> queryWrapper = new LambdaQueryWrapper();
            List<FUser> listData = fUserMapper.selectList(queryWrapper);
            List<FUserDTO> FUserDTOList = ListCopyUtil.copy(listData, FUserDTO.class);
        return FUserDTOList;
    }

    /**
     * 单个查询
     *
     * @param id the id of the entity
     * @return
     */
    @Override
    public FUserDTO findOne(Long id) {
        FUser  fUser =  fUserMapper.selectById(id);
        return  BeanConvertUtils.copyProperties(fUser,FUserDTO.class);
    }


    /**
     * 新增
     *
     * @param entityDTO the entity to create
     * @return
     */
    @Override
    @Transactional
    public FUserDTO create(FUserDTO entityDTO) {
       FUser fUser =  BeanConvertUtils.copyProperties(entityDTO,FUser.class);
        save(fUser);
        return  BeanConvertUtils.copyProperties(fUser,FUserDTO.class);
    }

    /**
     * 修改
     *
     * @param entity the entity to update
     * @return
     */
    @Override
    @Transactional
    public int update(FUserDTO entity) {
        FUser fUser = BeanConvertUtils.copyProperties(entity,FUser.class);
        return fUserMapper.updateById(fUser);
    }

    /**
     * 删除
     *
     * @param id the id of the entity
     */
    @Override
    @Transactional
    public int delete(Long id) {
        return fUserMapper.deleteById(id);
    }


    /**
    * 批量新增
    *
    */
    @Override
    @Transactional
    public Boolean insertBatch(List<FUserDTO> dataList) {
        List<FUser> result = ListCopyUtil.copy(dataList, FUser.class);
        return saveBatch(result);
    }


}
